package com.j.lemon.learn.basic.controller;

import com.google.common.util.concurrent.RateLimiter;
import com.j.lemon.learn.basic.annotations.Limiter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author lijunjun
 * @Date 2018-12-26 09:53
 * @Description
 */
@Controller
@RequestMapping(value = "api")
public class RateLimitTestController {

    private static final Logger logger = LoggerFactory.getLogger(RateLimitTestController.class);
    @RequestMapping(value = "limit")
    @Limiter(limit = 5,sec = 10)
    public String limit123(Model model){
//        if (RATE_LIMITER.tryAcquire()) {
//            System.out.println("1");
//        }else{
//            System.out.println("2");
//        }
//        double acquire = RATE_LIMITER.acquire();
//        System.out.println("execute sth ...");
        logger.info("execute something");
//        String a = null;
//        System.out.println(a.hashCode());
        model.addAttribute("username","lijunjun");
        return "success";
    }
}
